package com.jstyle.jclifexd.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.activity.GrowthMianRecordActivity;
import com.jstyle.jclifexd.model.RankData;
import com.jstyle.jclifexd.utils.DateUtil;
import com.jstyle.jclifexd.utils.ResolveData;

import java.text.NumberFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RankAdapter extends RecyclerViewBaseAdapter {
    public final static int ITEM_TYPE_TOP = 0;
    public final static int ITEM_TYPE_RANK = 1;
    public static final int type_days = 0;
    public static final int type_total = 1;
    int rankType = type_days;

    public RankAdapter(List<RankData.DataBean> dataBeans, int type) {
        super(dataBeans);
        this.rankType = type;
    }

    @Override
    protected void bindData(BaseViewHolder holder, int position) {
        final RankData.DataBean rankData = (RankData.DataBean) mDataList.get(position);
        RankViewHolder rankViewHolder = (RankViewHolder) holder;
        final Context context = rankViewHolder.itemView.getContext();
        rankViewHolder.tvRankLevel.setVisibility(position < 3 ? View.INVISIBLE : View.VISIBLE);
        rankViewHolder.tvRankLevel.setText(String.valueOf(position + 1));
        int textColor = 0;
        if (position < 3) {
            int id = context.getResources().getIdentifier("rank_" + (position + 1), "drawable", context.getPackageName());
            rankViewHolder.itemView.setBackgroundResource(id);
            textColor = Color.WHITE;
        } else {
            rankViewHolder.itemView.setBackgroundColor(Color.TRANSPARENT);
            textColor = context.getResources().getColor(R.color.rank_text_color);
        }
        rankViewHolder.tvRankName.setTextColor(textColor);
        rankViewHolder.tvRankDistance.setTextColor(textColor);
        rankViewHolder.tvRankStep.setTextColor(textColor);
        rankViewHolder.tvRankPace.setTextColor(textColor);
        rankViewHolder.tvRankDuration.setTextColor(textColor);
        rankViewHolder.tvRankHeart.setTextColor(textColor);

        rankViewHolder.tvRankName.setText(rankData.getNickname());
        double distance = rankData.getDistance();
        int pace = rankData.getPace();
        int minute = pace % 256;
        int second = pace / 256;
        NumberFormat numberFormat = ResolveData.getNumberFormat(2);
        rankViewHolder.tvRankDistance.setText(numberFormat.format(distance) + " " + context.getResources().getString(R.string.distance_unir));
        rankViewHolder.tvRankStep.setText(rankType == type_days ? String.valueOf(rankData.getStep()) : String.valueOf(rankData.getDays()));
        rankViewHolder.tvRankPace.setText(rankType == 0 ? DateUtil.getPaceTime(minute * 60 + second) : String.format("%1$02d'%2$02d\"", pace / 60, pace % 60));
        rankViewHolder.tvRankDuration.setText(DateUtil.getRankTime(rankData.getActiveTime(), context));

        Glide.with(context)
                .load(TextUtils.isEmpty(rankData.getAvatar()) ? context.getResources().getDrawable(R.drawable.rank_top) : rankData.getAvatar()).dontAnimate()
                .error(context.getResources().getDrawable(R.drawable.rank_top))
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(rankViewHolder.ivRankIcon);


        rankViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, GrowthMianRecordActivity.class);
                intent.putExtra("uid", rankData.getUid() + "");//my代表自己查看的数据
                intent.putExtra("uname", rankData.getNickname());//my代表自己查看的数据
                context.startActivity(intent);
            }
        });


    }


    @Override
    protected int getLayoutId(int viewType) {
        int layoutId = R.layout.item_rank;

        return layoutId;
    }

    public void updateData(List<RankData.DataBean> list) {
        this.mDataList = list;
        notifyDataSetChanged();
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(getLayoutId(viewType), parent, false);
        RankViewHolder rankViewHolder = new RankViewHolder(view);
        return rankViewHolder;
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    class RankViewHolder extends BaseViewHolder {
        @BindView(R.id.tv_rank_level)
        TextView tvRankLevel;
        @BindView(R.id.iv_rank_icon)
        ImageView ivRankIcon;
        @BindView(R.id.tv_rank_name)
        TextView tvRankName;
        @BindView(R.id.tv_rank_distance)
        TextView tvRankDistance;
        @BindView(R.id.tv_rank_step)
        TextView tvRankStep;
        @BindView(R.id.tv_rank_pace)
        TextView tvRankPace;
        @BindView(R.id.tv_rank_duration)
        TextView tvRankDuration;
        @BindView(R.id.tv_rank_heart)
        TextView tvRankHeart;

        RankViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
